<template>
  <el-dialog
    :title="!dataForm.oid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户编号" prop="oSno">
      <el-input v-model="dataForm.oSno" placeholder="用户编号"></el-input>
    </el-form-item>
    <el-form-item label="电单车编号" prop="oBid">
      <el-input v-model="dataForm.oBid" placeholder="单车编号"></el-input>
    </el-form-item>
    <el-form-item label="借车时间" prop="oBorrowTime">
      <el-input v-model="dataForm.oBorrowTime" placeholder="借车时间(格式：2022-02-02 00:00:00)"></el-input>
    </el-form-item>
    <el-form-item label="还车时间" prop="oReturnTime">
      <el-input v-model="dataForm.oReturnTime" placeholder="还车时间((格式：2022-02-02 00:00:00))"></el-input>
    </el-form-item>
    <el-form-item label="租金" prop="oRealRent">
      <el-input v-model="dataForm.oRealRent" placeholder="租金"></el-input>
    </el-form-item>
<!--    <el-form-item label="押金" prop="oCash">-->
<!--      <el-input v-model="dataForm.oCash" placeholder="押金"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="费用(2元/h)" prop="oMoney">
      <el-input v-model="dataForm.oMoney" placeholder="费用(2元/h)"></el-input>
    </el-form-item>
    <el-form-item label="单车状态" prop="oState">
      <el-input v-model="dataForm.oState" placeholder="单车状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="oCreateTime">
      <el-input v-model="dataForm.oCreateTime" placeholder="创建时间(格式:2020-02-02 00:00:00)"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="oUpdateTime">
      <el-input v-model="dataForm.oUpdateTime" placeholder="更新时间(格式:2020-02-02 00:00:00)"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="oComment">
      <el-input v-model="dataForm.oComment" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          oid: 0,
          oSno: '',
          oBid: '',
          oBorrowTime: '',
          oReturnTime: '',
          oRealRent: '',
          oCash: '',
          oMoney: '',
          oState: '',
          oCreateTime: '',
          oUpdateTime: '',
          oComment: ''
        },
        dataRule: {
          oSno: [
            { required: true, message: '用户编号不能为空', trigger: 'blur' }
          ],
          oBid: [
            { required: true, message: '单车编号不能为空', trigger: 'blur' }
          ],
          oBorrowTime: [
            { required: true, message: '借车时间不能为空', trigger: 'blur' }
          ],
          oReturnTime: [
            { required: true, message: '还车时间不能为空', trigger: 'blur' }
          ],
          oRealRent: [
            { required: true, message: '租金不能为空', trigger: 'blur' }
          ],
          oCash: [
            { required: true, message: '押金不能为空', trigger: 'blur' }
          ],
          oMoney: [
            { required: true, message: '费用(2元/h)不能为空', trigger: 'blur' }
          ],
          oState: [
            { required: true, message: '单车状态不能为空', trigger: 'blur' }
          ],
          oCreateTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          oUpdateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          oComment: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.oid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.oid) {
            this.$http({
              url: this.$http.adornUrl(`/bike/orders/info/${this.dataForm.oid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.oSno = data.orders.osno
                this.dataForm.oBid = data.orders.obid
                this.dataForm.oBorrowTime = data.orders.oborrowTime
                this.dataForm.oReturnTime = data.orders.oreturnTime
                this.dataForm.oRealRent = data.orders.orealRent
                this.dataForm.oCash = data.orders.ocash
                this.dataForm.oMoney = data.orders.omoney
                this.dataForm.oState = data.orders.ostate
                this.dataForm.oCreateTime = data.orders.ocreateTime
                this.dataForm.oUpdateTime = data.orders.oupdateTime
                this.dataForm.oComment = data.orders.ocomment
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bike/orders/${!this.dataForm.oid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'oid': this.dataForm.oid || undefined,
                'osno': this.dataForm.oSno,
                'obid': this.dataForm.oBid,
                'oborrowTime': this.dataForm.oBorrowTime,
                'oreturnTime': this.dataForm.oReturnTime,
                'orealRent': this.dataForm.oRealRent,
                'ocash': this.dataForm.oCash,
                'omoney': this.dataForm.oMoney,
                'ostate': this.dataForm.oState,
                'ocreateTime': this.dataForm.oCreateTime,
                'oupdateTime': this.dataForm.oUpdateTime,
                'ocomment': this.dataForm.oComment
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
